#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Push Status data                                                   #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 07/21/2016                                             #
# Last Modification: 07/21/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 85 
#
# MANUAL: This script will push all collected images and data to the status folder.
#
# DISPLAY: import_drift
# DISPLAY: iciness
# DISPLAY: defocus_RESMAX 
# DISPLAY: defocus_defocus
# DISPLAY: defocus_astig
# DISPLAY: defocus_phase_shift
# DISPLAY: defocus_CCvalue
# DISPLAY: frame_image_counts
# DISPLAY: import_original_time
# DISPLAY: flag_local_ctfresolutiontoobad_threshold
# DISPLAY: image_stddev_relative
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
set tempkeep = ""
set import_original_time = ""
set import_drift = ""
set iciness = ""
set defocus_RESMAX = ""
set defocus_defocus = ""
set defocus_astig = ""
set defocus_phase_shift = ""
set defocus_CCvalue = ""
set frame_image_counts = ""
set flag_local_ctfresolutiontoobad_threshold = ""
set image_stddev_relative = ""
#
#$end_vars
#
set scriptname = process_push_status
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
source ${proc_2dx}/2dx_makedirs
#
#The following line is important as the status parameters are in this file
source ~/.2dx/2dx.cfg
#
echo "<<@evaluate>>"
#
echo "<<@progress: 1>>"
#
# Do not change definition of "average", since this is also required in "push to status website" script.
set average = "../../microscope_average"
#
if(${import_original_time} == "-" || ${import_original_time} == "") then
   @ status_date = `date +%s` * 1000
   set date_text = "Updated on "`echo ${status_date} | awk ' { s = $1 / 1000 } END { print s } ' | ${app_gawk} '{print strftime("%c", $0)}' `
else
   set status_date = ${import_original_time}
   set date_text = "Updated on "`echo ${status_date} | awk ' { s = $1 / 1000 } END { print s } ' | ${app_gawk} '{print strftime("%c", $0)}' `
endif
#
if ( ${status_folder} != "none" ) then
  if ( ! -d ${status_folder} ) then
    \mkdir ${status_folder}
  endif
  #
  if ( ${status_folder_update} == "y" ) then
    ##########################################################################
    ${proc_2dx}/linblock "Update statuspage images."
    ##########################################################################
    #
    if ( -e  STATUS/1-image.jpg ) then
      \cp -f STATUS/1-image.jpg ${status_folder}/1-image.jpg
    else
      \cp -f ${proc_2dx}/Z-image.jpg ${status_folder}/1-image.jpg
    endif
    if ( -e  STATUS/1-FFT.jpg ) then
      \cp -f STATUS/1-FFT.jpg   ${status_folder}/1-FFT.jpg
    else
      \cp -f ${proc_2dx}/Z-FFT.jpg ${status_folder}/1-FFT.jpg
    endif
    if ( -e  STATUS/2-image.jpg ) then
      \cp -f STATUS/2-image.jpg ${status_folder}/2-image.jpg
    else
      \cp -f ${proc_2dx}/Z-image.jpg ${status_folder}/2-image.jpg
    endif
    if ( -e  STATUS/2-FFT.jpg ) then
      \cp -f STATUS/2-FFT.jpg   ${status_folder}/2-FFT.jpg
    else
      \cp -f ${proc_2dx}/Z-FFT.jpg ${status_folder}/2-FFT.jpg
    endif
    if ( -e  translations.png ) then
      \cp -f translations.png   ${status_folder}/3-image.png
    endif
    if ( -e  STATUS/4-image.jpg ) then
      \cp -f STATUS/4-image.jpg ${status_folder}/4-image.jpg
    else
      \cp -f ${proc_2dx}/Z-image.jpg ${status_folder}/4-image.jpg
    endif
    #
    if ( -e  ${average}_spectrum.mrc ) then
      \rm -f tmp.png
      \rm -f tmp2.png
      \rm -f STATUS/7-PS.jpg
      ${app_2dx_mrc_converter} --size 400 ${average}_spectrum.mrc tmp.png 
      ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 360 0 "Average session powerspectrum before drift correction (non-DW)"
      ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png STATUS/7-PS.jpg 10 375 0 "${date_text}"
      \cp -f STATUS/7-PS.jpg ${status_folder}/7-PS.jpg
    endif
    #
    if ( -e  ${average}_driftcor_spectrum.mrc ) then
      \rm -f tmp.png
      \rm -f tmp2.png
      \rm -f STATUS/8-PSdriftcor.jpg
      ${app_2dx_mrc_converter} --size 400 ${average}_driftcor_spectrum.mrc tmp.png 
      ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 360 0 "Average session powerspectrum after drift correction (non-DW)"
      ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png STATUS/8-PSdriftcor.jpg 10 375 0 "${date_text}"
      \cp -f STATUS/8-PSdriftcor.jpg ${status_folder}/8-PSdriftcor.jpg
    endif
    \rm -f tmp.png
    \rm -f tmp2.png
    #
    echo "${status_date};${defocus_defocus};${defocus_RESMAX};${frame_image_counts};${import_drift};${iciness};${defocus_CCvalue};${defocus_astig};${defocus_phase_shift};${image_stddev_relative}" >> ${status_folder}/data.log
    #
    echo "# IMAGE-IMPORTANT: STATUS/1-image.jpg <Image before drift correction>" >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: STATUS/1-FFT.jpg <Image before drift correction (FFT)>" >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: STATUS/2-image.jpg <Image after drift correction>" >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: STATUS/2-FFT.jpg <Image after drift correction (FFT)>" >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: translations.png <Translation Plot>" >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: STATUS/4-image.jpg <CTF Plot>" >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: STATUS/6-AverageImage.jpg <Average Image of Session>" >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: STATUS/7-PS.jpg <Average Powerspectrum of Session>" >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: STATUS/8-PSdriftcor.jpg <Average drift-corrected Powerspectrum of Session>" >> LOGS/${scriptname}.results
    set isgood = `echo ${defocus_RESMAX} ${flag_local_ctfresolutiontoobad_threshold} | awk '{ if ( $1 <= $2 ) { s = 1 } else { s = 0 } } END { print s }'`
    if ( ${isgood} == "1" ) then
      set currenttime = `date '+%s' | awk ' { s = $1 * 1000 } END { print s }'`
      echo "${currenttime}" >> ${status_folder}/last_good.txt
    endif
  endif
  #
endif
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
exit
#
# These are listed here to make sure they appear in the 2dx_image GUI:
#
